﻿/// <reference path="jquery-1.5.1.min.js" />

////////////////////////////////////////////////////////////////////////////////
//
//  Created by Fernando Cerqueira
//  Copyright (c) 2011.  All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
var preserveShutDown = "checked";
var currTheme = 1;
var WinMenuStyle = "8";
var WshShell = new ActiveXObject("WScript.Shell");
var FindWin8StarMenuKey = false;

// --------------------------------------------------------------------
function startup()
    // --------------------------------------------------------------------
{
    System.Gadget.settingsUI = "settings.html";
    System.Gadget.onSettingsClosed = settingsClosed;
    System.Gadget.Flyout.file = "Win8Keys.html";
    System.Gadget.Flyout.show = false;

    var SKey = "";
    var keymenu = "";
    var force = false;

    try {
        SKey = WshShell.RegRead("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RPEnabled");
        FindWin8StarMenuKey = true;
    }
    catch (e) {
        skey = "0";
    }

    if (SKey == "1") {
        keymenu = "8";
        }
    else {
        keymenu = "7";
    }

    if (keymenu != WinMenuStyle) {
        force = true;
    }

    var firstset = System.Gadget.Settings.read("firstset");
    if (firstset != "") {
        currTheme = System.Gadget.Settings.read("BackgroundTheme");
        preserveShutDown = System.Gadget.Settings.read("preserveShutDown");
    }
    else {
        //init default values
        System.Gadget.Settings.write("firstset", "1");
        System.Gadget.Settings.write("CheckAuto", true);
        System.Gadget.Settings.write("WinMenuStyle", keymenu);

        System.Gadget.Settings.write("preserveShutDown", preserveShutDown);
        System.Gadget.Settings.write("BackgroundTheme", currTheme);
    }
    System.Gadget.background = "images/bg_" + currTheme + ".png";
    changeMenu(keymenu, force);
}
// --------------------------------------------------------------------
function settingsClosed() {
    // --------------------------------------------------------------------
    if (currTheme != System.Gadget.Settings.read("BackgroundTheme")) {
        currTheme = System.Gadget.Settings.read("BackgroundTheme");
        System.Gadget.background = "images/bg_" + currTheme + ".png";
    }
    if (WinMenuStyle != System.Gadget.Settings.read("WinMenuStyle")) {
        changeMenu(System.Gadget.Settings.read("WinMenuStyle"), true);
    }
}

// --------------------------------------------------------------------
function changeMenu(startmenu,forceflip) {
    // --------------------------------------------------------------------
    if (startmenu == "8") {
        $("#divWin8").attr("class", "divWinVisible");
        $("#divWin7").attr("class", "divWinHide");
    }
    else {
        $("#divWin8").attr("class", "divWinVisible");
        $("#divWin7").attr("class", "divWinHide");
    }
    if (FindWin8StarMenuKey) {

        if (WinMenuStyle != startmenu || forceflip)
        {
            $("#divWin8").attr("class", "divWinVisible");
            $("#divWin7").attr("class", "divWinVisible");
            $('#flipbox').quickFlipper();
            WinMenuStyle = startmenu;
            System.Gadget.Settings.write("WinMenuStyle", WinMenuStyle);
        }
        if (WinMenuStyle == "8") {
            WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RPEnabled", 1, "REG_DWORD");
        }
        else if (WinMenuStyle == "7") {
            WshShell.RegWrite("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RPEnabled", 0, "REG_DWORD");
        }
    }
}

